package de.upb.aquarium.water.pump;

import javax.media.opengl.GL;

import de.upb.aquarium.World;
import de.upb.aquarium.api.objects.IPaintable;
import de.upb.aquarium.api.utility.VectorDouble;
import de.upb.aquarium.api.utility.VectorFloat;

public class WaterParticle implements IPaintable {
	public static double lifeTime;
	
	private static boolean renderControlPoints = false;

	private boolean bumper;
	private double[] controlPoints;
	private double startTime;
	
	public WaterParticle(double[] controlPoints, double startTime) {
		super();
		this.controlPoints = controlPoints;
		
		this.startTime = startTime;
	}
	
	private void setRandomColor(GL gl)
	{
		double rg = 0.25*Math.random() + 0.5;
		gl.glColor4d(rg, rg, rg + 0.15, Math.random()*0.5 + 0.25);
	}
	
	@Override
	public void paint(GL gl)
	{
		gl.glDisable(GL.GL_LIGHTING);
		gl.glEnable(GL.GL_BLEND);
		gl.glDisable(GL.GL_CULL_FACE);
		gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
		gl.glDepthMask(false);
		gl.glEnable(GL.GL_MAP1_VERTEX_3);
		gl.glMap1d(GL.GL_MAP1_VERTEX_3, 0.0, lifeTime, 3, 3, controlPoints, 0);
		gl.glMapGrid1d(20, 0.0, World.getInstance().getElapsedTime(startTime));
		
		setRandomColor(gl);
		gl.glEvalMesh1(GL.GL_LINE, 0, 20);

		gl.glDisable(GL.GL_BLEND);
		gl.glDepthMask(true);
		gl.glEnable(GL.GL_LIGHTING);
		
		if (renderControlPoints)
		{
			gl.glPointSize(3.0f);
			gl.glColor3d(1.0, 0.0, 0.0);
			gl.glBegin(GL.GL_POINTS);
				for (int i = 0; i < 9; i += 3)
				{
					gl.glVertex3dv(controlPoints, i);
				}
			gl.glEnd();
		}
	}
	
	public VectorFloat getPosition()
	{
		return new VectorFloat(new VectorDouble(controlPoints, 6, 3).data);
	}
	
	public boolean isAlive()
	{
		return (World.getInstance().getElapsedTime(startTime) <= lifeTime);		
	}
	
	public double[] getControlPoints()
	{
		return controlPoints;
	}
	
	public boolean isBumper() {
		return bumper;
	}

	public void setBumper(boolean bumper) {
		this.bumper = bumper;
	} 
	
	public static boolean isRenderControlPoints() {
		return renderControlPoints;
	}

	public static void setRenderControlPoints(boolean renderControlPoints) {
		WaterParticle.renderControlPoints = renderControlPoints;
	}
}
